package linked;

//反转链表
public class ReverseLinked {
    public static Node ReverseNode(Node head) {
        if (head == null) {
            return null;
        }
        //创建一个新的链表
        Node newNode = null;
        while (head != null) {
            //获取到头节点的下一个节点
            Node next = head.next;
            //将原来链表的头结点插入到新链表的头部
            head.next = newNode;
            //更新链表的头部
            newNode = head;
            head = next;
        }
        return newNode;
    }

    public static void main(String[] args) {
        Node node = new Node(1);
        node.next = new Node(2);
        node.next.next = new Node(3);
        node.next.next.next = new Node(4);
        node.next.next.next.next = new Node(5);

        Node temp=node;
        while (temp.next != null) {
            System.out.println(temp.date);
            temp = temp.next;
        }

        Node reverseNode = ReverseNode(node);

        Node temp2=reverseNode;
        while (temp2.next != null) {
            System.out.println(temp2.date);
            temp2 = temp2.next;
        }
    }
}
